declare var tt;
declare var wx;
import { _decorator, Component, Node } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('AdManager')
/**
 * 广告管理
 */
export class AdManager {

    //是否是微信环境
    public IsWx: boolean = false;

    //是否是抖音环境
    public IsTt: boolean = false;

    //广告信息配置
    private AdConfig: any = {
        Video: ["adunit-2ff7fa39b5de53b7"],
        CustomAd: "adunit-9562142bc58b6b97"
    }
    // private AdConfig: any = {
    //     Video: ["9q8q7p4aeh771hf909"],
    //     CustomAd: null
    // }

    //激励广告
    private RewardAd = {};

    //格子广告
    private CustomAd = null;

    //广告回调方法
    private AdCb: Function = null;

    //当前环境
    private Env: any = null;

    private constructor() {

        this.IsWx = (typeof wx != "undefined");
        this.IsTt = (typeof tt != "undefined");

        if (this.IsTt) this.IsWx = false;

        console.log("是否是微信环境", this.IsWx);
        console.log("是否是抖音环境", this.IsTt);

        if (this.IsTt) {
            tt.onShow((res) => {
                if (res.scene == "021036") {//侧边栏进入得

                }
            })
        }

        if (this.IsWx) {
            wx.showShareMenu({
                withShareTicket: true,
                menus: ['shareAppMessage', 'shareTimeline']
            })
        }

    }

    /**
     * 广告初始化
     */
    public Init() {

        //当前环境
        // this.Env = this.IsWx ? wx : tt;

        if (this.IsWx) this.Env = wx;
        if (this.IsTt) this.Env = tt;

        if (this.Env == null) return;

        //激励广告创建
        for (let id of this.AdConfig.Video) {
            let vidoad = this.Env.createRewardedVideoAd({ adUnitId: id });
            vidoad.onLoad(() => { app.logManager.debug("广告加载成成功") })
            vidoad.onError((err) => { app.logManager.debug("广告加载失败", err) })
            vidoad.onClose((res) => { this.AdCb(res.isEnded) })
            this.RewardAd[id] = vidoad;
        }

        //格子广告创建
        if (this.AdConfig.CustomAd) {
            this.CustomAd = this.Env.createCustomAd({
                adUnitId: this.AdConfig.CustomAd,
                style: {
                    left: 0,
                    top: 0,
                    width: 350
                }
            })
        }

    }

    /**
     * 播放激励视频
     */
    public ShowVideo(_index: number, _cb: Function) {
        app.topManager.NoTouchClick();
        if (this.Env == null) {
            _cb && _cb(true);
            return;
        }

        this.AdCb = _cb;
        this.RewardAd[this.AdConfig.Video[_index]].load().then(() => {
            this.RewardAd[this.AdConfig.Video[_index]].show().catch((err) => {
                app.logManager.debug("播放失败", err)
                this.VideoClose(false);
            });
        })
    }

    /**
     * 激励视频关闭
     */
    public VideoClose(_isEnd: boolean) {
        app.topManager.CanTouchClick();
        this.AdCb(_isEnd);
        this.AdCb = null;
    }

    /**
     * 分享
     */
    public ShareGame(_cb: Function) {
        if (this.IsWx) {
            this.Env.shareAppMessage({
                title: "快来开始吧",
                success: () => { console.log("分享成功") }
            })

            setTimeout(() => {
                _cb && _cb();
            }, 1000)
        } else {
            tt.shareAppMessage({
                success(res) {
                    _cb && _cb();
                },
                fail(e) {

                },
            });
        }

    }

    /**
     * 显示格子广告
     */
    public ShowCustomAd(_isshow: boolean) {

        if (this.Env == null) return;

        if (_isshow) {
            this.CustomAd.show();
        }
        else this.CustomAd.hide();
    }

    /** 单例实例 */
    public static readonly instance: AdManager = new AdManager();
}


export const adManager = AdManager.instance;
